#!/usr/bin/perl

use strict;

# this script goes through a set of sources in the directory
# and generates plots of each one, and then calls determine_spectral_index
# to find its spectral index, and writes it to a file

# call with sources as arguments

open(OUT,">spectral_indices.list");

for (my $i=0;$i<=$#ARGV;$i++){
    my $sourcename=$ARGV[$i];
    $sourcename=~s/\/$//; # remove trailing /
    # run uvspec
    my $miriad_command="uvspec";
    $miriad_command .= " vis=".$sourcename;
    $miriad_command .= " stokes=i interval=1440 options=nobase,avall";
    $miriad_command .= " axis=freq,amp device=/xs log=spectrum.txt";
    system $miriad_command;
    # run determine_spectral_index
    my $index_command = "determine_spectral_index";
    open(INDEX,"-|")||exec $index_command;
    while(<INDEX>){
	chomp;
	my $line=$_;
	if ($line=~/^amplitude/){
	    # this is the line we want
	    my @els=split(/\s+/,$line);
	    print OUT "$sourcename $els[2]\n";
	}
    }
    close(INDEX);
}
close(OUT);
